<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class WorkController extends AdminBaseController {

	//工作列表
	public function worklist(){
		$work = M("Work");
		$list = $work->where()->order('id desc')->select();
    	$data['count'] = count($list);
        $user = new \Admin\Controller\UserController();
        $planner = new \Admin\Controller\PlannerController();
    	for ($i=0; $i < $data['count']; $i++) { 
            $arr = explode(',',$list[$i]['users']);
            for ($j=0; $j < count($arr); $j++) {
                $arr[$j] = $user->getusernamebyid($arr[$j]);
            }
            $list[$i]['users'] = implode(',',$arr);
            //发布者
            $list[$i]['plannerid'] = $planner->getplnamebyid($list[$i]['plannerid']);
            
    	}
    	$this->assign('work',$list);
    	$this->assign('data',$data);
    	$this->display("./Public/AdminLTE/pages/tables/worklist.html");
	}

    //ajax获取工作
    public function ajaxgetwork(){
        $work = M("Work");
        $list = $work->where()->order('id desc')->select();
        $this->ajaxReturn($list);
    }

    //ajax根据工作ID获取工作可读性信息
    public function getworksbyid($id=0){
        $work = M("Work");
        $res = $work->where('id='.$id)->find();
        if ($res) {
            $planner = new \Admin\Controller\PlannerController();
            $res['plannerid'] = $planner->getplnamebyid($res['plannerid']);
            $users = explode(',',$res['users']);
            $user = new \Admin\Controller\UserController();
            for ($i=0; $i < count($users); $i++) { 
                $users[$i] = $user->getusernamebyid($users[$i]);
            }
            $res['users'] = implode(',', $users);
            $this->ajaxReturn($res);
        }
    }

	//发布工作表单
    public function addworkform(){
    	$workgroup = new \Admin\Controller\WorkgroupController();
        $group=$workgroup->getallgroup();
        $this->assign('group',$group);
    	$this->display("./Public/AdminLTE/pages/forms/addwork.html");
    }

    //发布工作
    public function addwork(){
    	if (I('post.title')!="") {$data['title'] = I('post.title');}else{exit;}
    	if (I('post.timesta')!="") {$data['timesta'] = I('post.timesta');}else{exit;}
    	if (I('post.timeend')!="") {$data['timeend'] = I('post.timeend');}else{exit;}
    	if (I('post.users')!="") {$data['users'] = I('post.users');}else{exit;}
    	if (I('post.planner')!="") {$data['plannerid'] = I('post.planner');}else{exit;}
    	if (I('post.remark')!="") {$data['remark'] = I('post.remark');}
        $today = date("Y-m-d");
        if ($today<$data['timesta']) {
            $data['sta'] = 1;//未开始
        }elseif($today>=$data['timesta'] && $today<=$data['timeend']){
            $data['sta'] = 2;//工作中
        }elseif($today>$data['timeend']){
            $data['sta'] = 3;//完结
        }
    	$work = M("Work");
    	$res = $work->data($data)->filter('strip_tags')->add();
    	if ($res) {
    		$this->success('发布成功');
    	}else{
    		$this->error("发布失败");
    	}
    }

    //更新所有工作状态
    public function updateallwork(){
        $work = M("Work");
        //$work->order('id desc')->select();
    }

    public function test(){
        echo "a:2017-07-25<br>";
        echo "b:2017-08-05<br>";
        $a = "2017-07-25";
        $b = "2017-08-05";
        if ($a>$b) {
            echo "a大于B";
        }
        if($a<$b){
            echo "a小于b<br>";
            echo $a-$b;
        }
        echo date("Y-m-d");
    }

}